<template>
  <div class="statistic-card" :class="{ 'clickable': clickable}">
    <el-icon v-show="clickable" class="icon-view"><Promotion /></el-icon>
    <el-statistic :value="value" :precision="precision" :prefix="valuePrefixUnit" :suffix="valueSuffixUnit">
      <template #title>
        <div style="display: inline-flex; align-items: center; font-size: 18px;">
          {{ title }}
          <el-tooltip
            effect="dark"
            placement="top"
            :hide-after="100"
            v-if="titleTooltipText"
          >
            <template #content>
              <div class="title-tooltip-text">{{ titleTooltipText }}</div>
            </template>
            <el-icon size="14" style="margin-left: 4px;">
              <Warning></Warning>
            </el-icon>
          </el-tooltip>
        </div>
      </template>
      <template #prefix>
        <span v-if="valuePrefixUnit" class="value-unit">
          <span v-if="valuePrefixUnit.startsWith('icon:')">
            <component :is="valuePrefixUnit.substring(5)"></component>
          </span>
          <span v-else>
            {{ valuePrefixUnit }}
          </span>
        </span>
      </template>
      <template #suffix>
        <span v-if="valueSuffixUnit" class="value-unit">
          <span v-if="valueSuffixUnit.startsWith('icon:')">
            <component :is="valueSuffixUnit.substring(5)"></component>
          </span>
          <span v-else>
            {{ valueSuffixUnit }}
          </span>
        </span>
      </template>
    </el-statistic>
    <div class="statistic-footer" v-if="change">
      <div class="footer-item">
        <span>较上一统计日</span>
        <span :class="isPositiveChange ? 'green' : 'red'">
          {{ change }}
          <el-icon>
            <CaretTop v-if="isPositiveChange" />
            <CaretBottom v-else />
          </el-icon>
        </span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineProps, computed } from 'vue';
import { CaretBottom, CaretTop, Promotion, Warning } from '@element-plus/icons-vue'

const { title, titleTooltipText, value, valuePrefixUnit = '', valueSuffixUnit = '', change, isPositiveChange = false, type } = defineProps({
  title: String,
  titleTooltipText: String,
  value: Number,
  valueSuffixUnit: String,
  valuePrefixUnit: String,
  type: String,
  change: String,
  isPositiveChange: Boolean,
  clickable: Boolean
});

const precision = computed(() => {
  switch (type) {
    case 'money':
      return 2;
    default:
      return 0;
  }
});
</script>



<style scoped>
.el-statistic {
  --el-statistic-content-font-size: 38px;
}

.icon-view {
  position: absolute;
  right: 1em;
  top: 1em;
  color: rgb(121.3, 187.1, 255);
  font-weight: bold;
}

.value-unit {
  font-size: 18px;
}

.title-tooltip-text {
  width: 300px;
}

.statistic-card {
  position: relative;
  text-align: center;
  height: 100%;
  min-height: 5em;
  width: calc(100% - 30px);
  padding: 30px 15px 5px 15px;
  border-radius: 4px;
  background-color: #F0F2F5;
}
.clickable:hover {
  cursor: pointer;
}

.statistic-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  font-size: 14px;
  color: #303133;
  margin-top: 16px;
}

.statistic-footer .footer-item {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.statistic-footer .footer-item span:last-child {
  display: inline-flex;
  align-items: center;
  margin-left: 4px;
}

.green {
  color: #67C23A;
}
.red {
  color: #F56C6C;
}
</style>
